package com.riggie.extension.listener;

import com.rabbitmq.client.Channel;
import com.riggie.extension.constant.Constants;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class QOSListener {

    //@RabbitListener(queues = Constants.QOS_QUEUE)
    public void handMessage(Message message, Channel channel) throws IOException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        System.out.printf("接收到消息： %s，deliveryTag: %d \n",new String(message.getBody()),deliveryTag);

        //channel.basicAck(deliveryTag,true);
    }

    //@RabbitListener(queues = Constants.QOS_QUEUE)
    public void handMessage1(Message message, Channel channel) throws IOException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        try {
            //消费者逻辑
            System.out.printf("[消费者1]接收到消息： %s，deliveryTag: %d \n",new String(message.getBody()),deliveryTag);
            Thread.sleep(1000);
            //肯定确认
            channel.basicAck(deliveryTag,true);

        } catch (InterruptedException e) {
            //否定确认
            channel.basicNack(deliveryTag,false,true);
        }
    }

    //@RabbitListener(queues = Constants.QOS_QUEUE)
    public void handMessage2(Message message, Channel channel) throws IOException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        try {
            //消费者逻辑
            System.out.printf("[消费者2]接收到消息： %s，deliveryTag: %d \n",new String(message.getBody()),deliveryTag);
            Thread.sleep(2000);
            //肯定确认
            channel.basicAck(deliveryTag,true);

        } catch (InterruptedException e) {
            //否定确认
            channel.basicNack(deliveryTag,false,true);
        }
    }

}
